package com.songcy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.ObjectDao;
import com.pro.bean.Card_View;
import com.pro.bean.Info;
import com.pro.bean.Mark;
import com.pro.bean.Unit;
import com.pro.bean.Users;
import com.songcy.dao.CardDao;

public class GetCardServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String name = new String(request.getParameter("name").getBytes(
				"ISO-8859-1"), "UTF-8");
		String cardname = new String(request.getParameter("cardname").getBytes(
				"ISO-8859-1"), "UTF-8");
		String cmp = new String(request.getParameter("cmp").getBytes(
				"ISO-8859-1"), "UTF-8");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
		String start = request.getParameter("dates");
		String end = request.getParameter("datee");
		String arg = new String(request.getParameter("datearg").getBytes(
				"ISO-8859-1"), "UTF-8");

		CardDao carddao = new CardDao();
		List rs = null;
		if (name.length() != 0 && !name.equals("my"))
			rs = carddao.findCards(cmp, cardname, name, arg, start, end);
		else if(name.equals("my"))
		{
			System.out.println("ooooooooooooooooooooooooo "+((Users) request.getSession()
					.getAttribute("Users")).getUsername());
			rs = carddao.findCards(cmp, cardname, ((Users) request.getSession()
					.getAttribute("Users")).getUsername(), arg, start, end);
		}
		Iterator it = rs.iterator();
		out.println("<!DOCTYPE HTML><head></head><body>");
		String json = "([";
		List l = null;
		List finalrs = new ArrayList();
		ObjectDao objectdao = new ObjectDao();
		Users u = (Users)request.getSession().getAttribute("Users");
		while (it.hasNext()) 
		{
			Card_View cardview = (Card_View) it.next();
			l = objectdao.find(cardview.getCardid(), new Mark());
			Mark m = (Mark) l.get(0);
			if (m.getIspublic() == 1 || m.getOwuserid()==u.getUserid())
			{
				finalrs.add(cardview);
			}
		}
		json += "])";
		out.println(json);
		out.println("</body>");
		request.getSession().setAttribute("viewlist", finalrs);
		// System.out.println(json);
		request.getRequestDispatcher("../scypage/table.jsp").forward(request,
				response);
		// try {
		// Date start =(Date)sdf.parse(request.getParameter("dates"));
		// Date end =(Date)sdf.parse(request.getParameter("datee"));
		// } catch (ParseException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

		// out.flush();
		// out.close();
	}

	private boolean checkpublic(int cardid) {
		ObjectDao objectdao = new ObjectDao();
		List l = objectdao.find(cardid, new Mark());
		Mark m = (Mark) l.get(0);
		if (m.getIspublic() == 1)
			return true;
		else
			return false;
	}

}
